/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Vistas;

/**
 *
 * @author 18204240-4
 */

import javax.swing.table.DefaultTableModel;
import Modelo.Ciudades;
import Modelo.Funciones;
public class Ciudad extends javax.swing.JInternalFrame {

    /**
     * Creates new form Ciudad
     */
    
    DefaultTableModel modeloTbCiudades = new DefaultTableModel();
    public Ciudad() {
        initComponents();
        this.poblarTbCiudades();
    }
 private void poblarTbCiudades() {
        
    
    modeloTbCiudades = new DefaultTableModel() {
            @Override
            public boolean isCellEditable(int row, int col) {
                return false;
            }
        };
        //Cabeceras    
        modeloTbCiudades.addColumn("ID");
        modeloTbCiudades.addColumn("Ciudad");
        
        this.desactivar();

        //Body, obtengo los clientes
        Ciudades c = new Ciudades();
        for (Object[] ciudad : c.getCiudades()) {
           
            
            modeloTbCiudades.addRow(ciudad);
        }
     
        tbCiudades.setModel(modeloTbCiudades);
       
    }
 
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbCiudades = new javax.swing.JTable();
        btnLimpiar = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnQuitar = new javax.swing.JButton();
        txtciudad = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setTitle("Ciudades");

        tbCiudades.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbCiudades.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbCiudadesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbCiudades);

        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnAdd.setText("Agregar");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setText("Modificar");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnQuitar.setText("Quitar");
        btnQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnQuitarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(114, 114, 114)
                                .addComponent(txtciudad, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(38, 38, 38)
                                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(31, 31, 31)
                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                                .addComponent(btnQuitar, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(89, 89, 89))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 315, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addComponent(txtciudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAdd)
                            .addComponent(btnUpdate)
                            .addComponent(btnQuitar))
                        .addGap(41, 41, 41)
                        .addComponent(btnLimpiar)))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        // TODO add your handling code here:
         txtciudad.setText("");
       this.poblarTbCiudades();
        
    }//GEN-LAST:event_btnLimpiarActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        if (tbCiudades.getSelectedRow() > -1) {
        
           if (txtciudad.getText().trim().length() == 0) {
            Funciones.mensaje("Debe ingresar el nombre de la ciudad");
            
            }
           else {
               
            int id = Integer.parseInt(String.valueOf(tbCiudades.getValueAt(tbCiudades.getSelectedRow(), 0)));
            Ciudades c = new Ciudades();
            String ciudad = txtciudad.getText();
            c.updateCiudad(ciudad, id);
    
           }
            this.poblarTbCiudades();
            txtciudad.setText("");
            this.poblarTbCiudades();
        }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
          if ((txtciudad.getText().trim().length() == 0)) {
            Funciones.mensaje("Debe ingresar el nombre de la ciudad");
        } else {
            
        Ciudades c = new Ciudades();
        String ciudad= txtciudad.getText();
        
        c.addCiudad(ciudad);
        this.poblarTbCiudades();
        txtciudad.setText("");
       
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnQuitarActionPerformed
        // TODO add your handling code here:
         if (tbCiudades.getSelectedRow() > -1) {
            int id = Integer.parseInt(String.valueOf(tbCiudades.getValueAt(tbCiudades.getSelectedRow(), 0)));
            String ciudad = String.valueOf(tbCiudades.getValueAt(tbCiudades.getSelectedRow(), 1));
            Ciudades c = new Ciudades ();
            if (c.existeCiudad(ciudad)==false){
            c.deleteCiudades(id);
            this.poblarTbCiudades();
            txtciudad.setText("");
            }
            else {
            Funciones.mensaje("La ciudad no existe");
            this.poblarTbCiudades();
            }
            
                
        }
    }//GEN-LAST:event_btnQuitarActionPerformed

    private void tbCiudadesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbCiudadesMouseClicked
        // TODO add your handling code here:
        this.activar();
        if (tbCiudades.getSelectedRow() > -1) {
            
            txtciudad.setText(String.valueOf(tbCiudades.getValueAt(tbCiudades.getSelectedRow(), 1)));
            btnAdd.setEnabled(false);
            
        }
    }//GEN-LAST:event_tbCiudadesMouseClicked
private void desactivar (){
   
    btnQuitar.setEnabled(false);
    
    btnUpdate.setEnabled(false);
    
    }
    private void activar (){
    
    btnQuitar.setEnabled(true);
    
    btnUpdate.setEnabled(true);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnQuitar;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbCiudades;
    private javax.swing.JTextField txtciudad;
    // End of variables declaration//GEN-END:variables
}
